package com.guli.eduService.controller;


import com.guli.commonutils.R;
import com.guli.eduService.entity.EduChapter;
import com.guli.eduService.entity.chapter.ChapterVo;
import com.guli.eduService.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author LMZovo
 * @since 2022-02-15
 */
@Api(tags = "课程章节-存储课程章节信息")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "课程大纲列表") //要根据课程id进行查询
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable("courseId") String courseId) {
        List<ChapterVo> result = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", result);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        chapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "根据章节id进行查找")
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable("chapterId") String chapterId) {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }


    @ApiOperation(value = "修改章节")
    @GetMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "删除章节")
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable("chapterId") String chapterId) {
        boolean isDelete = chapterService.deleteChapter(chapterId);
        if (isDelete) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}